var  teus="0";
var ctx7_labels=[];
var ctx7_data=[];
var ctx7_labels1=[];
var ctx7_data1=[];
var labels1=[];
var datas=[];
var labels6=[];
var datas6=[];
var labels2=[];
var	datas2=[];
var datas22=[];
var labels4=[];
var datas4=[];
var total=0;
var four=0;
var total1=0;
var four1=0;
var len="";
var portname = localStorage.getItem("portname");
//alert(portname);
var parameter= {"startTime":"2017-01-01","port":portname};
$(function(){
	
	Chart.defaults.global.defaultFontColor = '#fff';
	$("#maptitle").html("当前港口："+portname);
	
	//客户订舱量前十名
	customerTopTen();
	//七日总订舱量
    foreignBookingQuantity();
    //货物比重(全国)
    goodsCNSpecificWeight();
    //货物比重(办事处)
    goodsOrgSpecificWeight();
	//吞吐量港口排名
	throughputPortRanking();
	//航线贡献值
	routeContribution();
	//月份货物重量
	foreignMonthBookingQuantity();
	//外贸总订舱量
	foreignBookingQuantityAll();
	//应收达标率
  	standardReceivableRate(); 	
  	//箱量指标达标率
	boxTargetComplianceRate();
})
//箱量指标达标率
var boxTargetComplianceRate  =function(){
	$.ajax({
        type: "post",
        async: true,
        url: "/enReport/boxTargetComplianceRate",
        data: {"year":2017,"portname":portname},
        dataType: "json",
        success: function(data){
        	var datavalue ="";
        	for(var i=0; i<data.length;i++){
        		datavalue=data[0].protpercent;
        		i=data.length;
        	}
        	var data1 =parseInt(datavalue);
       		var gauge_1 = echarts.init(document.getElementById("gauge_1"));
			option_gauge_1 = {
			    tooltip: {
			    	show : false
			    },
			    series: [
			        {
			            name: '完成率',
			            type: 'gauge',
			            radius:'100%',
			            startAngle:180,
			            axisLine:{
			            	lineStyle:{
			            		width:10,
			            		color:[[0.2,'#0193cb'], [0.8,'#2fc5ff'], [1,'#92e1ff']],
			            	},
			            },
			            splitLine:{
			            	length:5,
			            },
			            axisTick:{
			            	show:false,
			            	length:'5%',
			            },
			            title : {
			                show : false,
			                offsetCenter: ['-65%', '30%'],
			                textStyle: {
			                    color: '#ccc',
			                    fontSize : 15
			                }
			            },
			            detail: {
			            	color: '#ccc',
			            	fontSize:22,
			            	formatter:'{value}%',
			            	offsetCenter:['-50%', '60%'],
			            },
			            data: [{value: data1, name: '箱量指标达标率'}]
			        }
			    ]
			};
			 gauge_1.setOption(option_gauge_1, true);
			setInterval(function () {
				$.ajax({
			        type: "post",
			        async: true,
			        url: "/enReport/boxTargetComplianceRate",
			        data: {"year":2017,"portname":portname},
			        dataType: "json",
			        success: function(data){
			        	var datavalue1 ="";
			        	for(var i=0; i<data.length;i++){
			        		datavalue1=data[0].protpercent;
			        		i=data.length;
			        	}
			        	var data2 =parseInt(datavalue1);
			        	option_gauge_1.series[0].data[0].value = data2;
			   			 gauge_1.setOption(option_gauge_1, true);
					}
				})
				},3000);
					}
				})
}
//应收达标率
var standardReceivableRate  =function(){
	$.ajax({
        type: "post",
        async: true,
        url: "/enReport/standardReceivableRate",
        data: {"year":2017,"portname":portname},
        dataType: "json",
        success: function(data){
        	var datavalue ="";
        	for(var i=0; i<data.length;i++){
        		datavalue=data[0].portpercent;
        		i=data.length;
        	}
        	var data1 =parseInt(datavalue);
       		var gauge_2 = echarts.init(document.getElementById("gauge_2"));
			option_gauge_2 = {
			    tooltip: {
			    	show : false
			    },
			    series: [
			        {
			            name: '完成率',
			            type: 'gauge',
			            radius:'100%',
			            startAngle:180,
			            axisLine:{
			            	lineStyle:{
			            		width:10,
			            		color:[[0.2,'#0193cb'], [0.8,'#2fc5ff'], [1,'#92e1ff']],
			            	},
			            },
			            splitLine:{
			            	length:5,
			            },
			            axisTick:{
			            	show:false,
			            	length:'5%',
			            },
			            title : {
			                show : false,
			                offsetCenter: ['-65%', '30%'],
			                textStyle: {
			                    color: '#ccc',
			                    fontSize : 15
			                }
			            },
			            detail: {
			            	color: '#ccc',
			            	fontSize:22,
			            	formatter:'{value}%',
			            	offsetCenter:['-50%', '60%'],
			            },
			            data: [{value: data1, name: '箱量指标达标率'}]
			        }
			    ]
			};
			 gauge_2.setOption(option_gauge_2, true);
			setInterval(function () {
				$.ajax({
			        type: "post",
			        async: true,
			        url: "/enReport/standardReceivableRate",
			        data: {"year":2017,"portname":portname},
			        dataType: "json",
			        success: function(data){
			        	var datavalue1 ="";
			        	for(var i=0; i<data.length;i++){
			        		datavalue1=data[0].portpercent;
			        		i=data.length;
			        	}
			        	var data2 =parseInt(datavalue1);
			        	option_gauge_2.series[0].data[0].value = data2;
			   			 gauge_2.setOption(option_gauge_2, true);
					}
				})
				},3000);
					}
				})
}
//七日总订舱量
var foreignBookingQuantity=function(){
	 $.ajax({
	        type: "post",
	        async: true,
	        url: "/enReport/foreignBookingQuantity",
	        data: parameter,
	        dataType: "json",
	        success: function(data){
	        	labels4=[];
	        	datas4=[];
	        	for(var i=0;i<data.length;i++){
	        		labels4.push(data[i].d);
	        		datas4.push(data[i].teu);
	        	}
		 		var ctx4 = document.getElementById('chart4').getContext('2d');
				var myChart4 = new Chart(ctx4, {
				type: 'line',
				data: {
					labels: labels4,
					datasets: [{
							label: "订舱量",
							borderWidth: '3',
							backgroundColor: "rgba(94,151,200,0.5)",
							borderColor: "rgba(94,151,200,1)",
							pointBackgroundColor: "#46ffc3",
							pointStrokeColor: "#fff",
							data: datas4
						}
					]
				},
				options: {
					legend: {
						labels: {
						 
						},
					},
					scales: {
						yAxes: [{
							stacked: false
						}]
					}
				},
			});
			setInterval(function () {
				$.ajax({
			        type: "post",
			        async: true,
			        url: "/enReport/foreignBookingQuantity",
			        data: parameter,
			        dataType: "json",
			        success: function(data){
			        	labels4=[];
	        			datas4=[];
			        	for(var i=0;i<data.length;i++){
			        		labels4.push(data[i].d);
			        		datas4.push(data[i].teu);
			        	}
			        	myChart4.data.datasets.forEach((dataset) => {
		 			  		data.labels=labels4;
		 			        dataset.data =datas4;
					    });
					}
				})
	    		myChart4.update();
		}, 3600000);
        	}
    });
}
//月份货物重量
var  foreignMonthBookingQuantity=function(){
	$.ajax({
        type: "post",
        async: true,
        url: "/enReport/foreignMonthBookingQuantity",
        data: parameter,
        dataType: "json",
        success: function(data){
        	datas22=[];
         	for(var j=0;j<data.length;j++){
         		datas22.push(data[j].teu);
         	}
         	foreignCargoWeight();
		}
	})
 
}

var foreignCargoWeight =function(){
		$.ajax({
        type: "post",
        async: true,
        url: "/enReport/foreignCargoWeight",
        data: parameter,
        dataType: "json",
        success: function(data){
        	labels2=[];
        	datas2=[];
        	for(var i=0;i<data.length;i++){
        		datas2.push(data[i].cfweight);
	     		labels2.push(data[i].mm);
        	}
        	var ctx2 = document.getElementById('chart2').getContext('2d');
			var myChart2 = new Chart(ctx2, {
				type: 'bar',
				data: {
					labels: labels2,
					datasets: [{
							label: "货物重量",
							borderWidth: '3',
							backgroundColor: "rgba(94,151,200,0.5)",
							borderColor: "rgba(94,151,200,1)",
							pointBackgroundColor: "#46ffc3",
							pointStrokeColor: "#fff",
							data: datas2,
							yAxisID: 'left-y-axis',
					},
					{
							label: "订舱量",
							borderWidth: '3',
							backgroundColor: "rgba(212,228,249,0.5)",
							borderColor: "rgba(212,228,249,1)",
							pointBackgroundColor: "#46ffc3",
							pointStrokeColor: "#fff",
							data: datas22,
							yAxisID: 'right-y-axis',
						}
					]
				},
				options: {
					scales: {
			            yAxes: [{
			                id: 'left-y-axis',
			                position: 'left',
			                ticks: {
			                    callback: function(value, index, values) {
			                        return (value/1000)+'k';
			                    }
			                }
			            },{
			                id: 'right-y-axis',
			                position: 'right'
			            }]
			        },
					legend: {
						labels: {
							//	    			defaultFontColor: '#ffffff',
							//		            fontColor: '#ffffff',
						},
						//	            display: false,
					},
				},
			});
			setInterval(function () {
					$.ajax({
				        type: "post",
				        async: true,
				        url: "/enReport/foreignCargoWeight",
				        data: parameter,
				        dataType: "json",
				        success: function(data){
				        	labels2=[];
				        	datas2=[];
				        	for(var i=0;i<data.length;i++){
				        		datas2.push(data[i].cfweight);
					     		labels2.push(data[i].mm);
				        	}
				        	$.ajax({
						        type: "post",
						        async: true,
						        url: "/enReport/foreignMonthBookingQuantity",
						        data: parameter,
						        dataType: "json",
						        success: function(data){
						        	datas22=[];
						        	for(var j=0;j<data.length;j++){
						        		datas22.push(data[j].teu);
							      
						        	}
						        	myChart2.data.datasets.forEach((dataset) => {
					 			  		data.labels=labels2;
					 			        myChart2.data.datasets[0].data =datas2;
					 			        myChart2.data.datasets[1].data =datas22;
								    });
								}
							})
						}
					})
			    myChart2.update();
			}, 3600000);
			
		}
	})
}
//航线贡献值
var routeContribution =function(){
	$.ajax({
        type: "post",
        async: true,
        url: "/enReport/routeContribution",
        data: parameter,
        dataType: "json",
        success: function(data){
        	labels6=[];
        	datas6=[];
        	for(var i=0;i<data.length;i++){
        		labels6.push(data[i].service_name);
        		datas6.push(data[i].teu);
        	}
	       	var ctx6 = document.getElementById('chart6').getContext('2d');	
			var myChart6 = new Chart(ctx6, {
				type: 'bar',
				data: {
					labels: labels6,
					datasets: [{
						label: "数据一",
						borderWidth: '3',
						backgroundColor: "rgba(94,151,200,0.5)",
						borderColor: "rgba(94,151,200,1)",
						pointBackgroundColor: "#46ffc3",
						pointStrokeColor: "#fff",
						data: datas6
					}]
				},
				options: {
					scales: {
				        xAxes: [{
				            ticks: {
				            	autoSkip: false,
				            }
				        }]
				    },
					maintainAspectRatio: false,
					legend: {
						labels: {
							//	    			defaultFontColor: '#ffffff',
							//		            fontColor: '#ffffff',
						},
						display: false,
					},
				},
			});
			setInterval(function () {
					$.ajax({
				        type: "post",
				        async: true,
				        url: "/enReport/routeContribution",
				        data: parameter,
				        dataType: "json",
				        success: function(data){
				        	labels6=[];
				        	datas6=[];
				        	for(var i=0;i<data.length;i++){
				        		labels6.push(data[i].service_name);
				        		datas6.push(data[i].teu);
				        	}
						}
					})
			  	myChart6.data.datasets.forEach((dataset) => {
			  		data.labels=labels6;
			        dataset.data =datas6;
			    });
			    myChart6.update();
			}, 3600000);
    	}
    });
}
//吞吐量港口排名
var throughputPortRanking =function(){
	$.ajax({
	        type: "post",
	        async: true,
	        url: "/enReport/monthlyReceivable",
	        data: parameter,
	        dataType: "json",
	        success: function(data){
	        	labels1=[];
	        	datas=[];
	        	for(var i=0;i<data.length;i++){
	        		if(i<5){
	        			labels1.push(data[i].month)
	        			datas.push(data[i].amount);
	        		}
	        	}
		    	var ctx3 = document.getElementById('chart3').getContext('2d');
				var myChart3 = new Chart(ctx3, {
					type: 'bar',
					data: {
						labels:labels1,
						datasets: [{
							label: "港口月度应收",
							borderWidth: '2',
							backgroundColor: [
								"rgba(212,228,249,0.5)",
								"rgba(25,198,198,0.5)",
								"rgba(202,127,50,0.5)",
								"rgba(94,151,200,0.5)",
								"rgba(206,120,222,0.5)",
								"rgba(94,151,200,0.5)"
							],
							borderColor: [
								"rgba(212,228,249,1)",
								"rgba(25,198,198,1)",
								"rgba(202,127,50,1)",
								"rgba(94,151,200,1)",
								"rgba(206,120,222,1)",
								"rgba(94,151,200,1)"
							],
							pointBackgroundColor: "#46ffc3",
							pointStrokeColor: "#fff",
							data: datas
						}]
					},
					options: {
						scales: {
				            yAxes: [{
				                ticks: {
				                    callback: function(value, index, values) {
				                        return (value/1000)+'k';
				                    }
				                }
				            }]
				        },
						maintainAspectRatio: false,
						legend: {
							labels: {
								//	    			defaultFontColor: '#ffffff',
								//		            fontColor: '#ffffff',
							},
							display: false,
						},
					},
				});
			 	setInterval(function () {
					$.ajax({
				        type: "post",
				        async: true,
				        url: "/enReport/throughputPortRanking",
				        data: parameter,
				        dataType: "json",
				        success: function(data){
				        	labels1=[];
	        				datas=[];
						   	for(var i=0;i<data.length;i++){
						   		if(i<5){
						   			labels1.push(data[i].month)
				        			datas.push(data[i].amount);
						   		}
				        	}
						}
					})
			  	myChart3.data.datasets.forEach((dataset) => {
			  		data.labels=labels1;
			        dataset.data =datas;
			    });
			    myChart3.update();
			}, 3600000);
        	}
    });
}
//货物比重(办事处)
var goodsOrgSpecificWeight =function(){
	    $.ajax({
	        type: "post",
	        async: true,
	        url: "/enReport/boxRatio",
	        data: {"startTime":2017-01,"port":portname},
	        dataType: "json",
	        success: function(data){
			    ctx7_labels1=[];
			    ctx7_data1=[];
			     for(var i=0; i<data.length;i++){
			     		ctx7_labels1.push(data[i].boxtype);
			     		ctx7_data1.push(data[i].teu);
			     	 
			     }
			    var ctx8 = document.getElementById('chart8').getContext('2d');
				var myChart8 = new Chart(ctx8, {
					type: 'pie',
					data: {
						labels: ctx7_labels1,
						datasets: [{
							data: ctx7_data1,
							backgroundColor: [
								"rgba(94,151,200,0.5)",
								"rgba(212,228,249,0.5)",
								"rgba(25,198,198,0.5)",
								"rgba(202,127,50,0.5)",
								"rgba(206,120,222,0.5)",
							],
							borderColor: [
								"rgba(94,151,200,1)",
								"rgba(212,228,249,1)",
								"rgba(25,198,198,1)",
								"rgba(202,127,50,1)",
								"rgba(206,120,222,1)",
							],
							borderWidth: '2',
							pointRadius: '10',
							hoverBorderWidth: '0',
							hoverBorderColor: ['#2185da', '#2185da', '#2185da'],
						}],
					},
					options: {
						legend: {
							display: true,
						},
					},
				});
					setInterval(function () {
						$.ajax({
					        type: "post",
					        async: true,
					        url: "/enReport/boxRatio",
					        data: {"startTime":2017-01,"port":portname},
					        dataType: "json",
					        success: function(data){
							    ctx7_labels1=[];
							    ctx7_data1=[];
							    total1=0;
				   				four1=0;
							     for(var i=0; i<data.length;i++){
							     		ctx7_labels1.push(data[i].boxtype);
							     		ctx7_data1.push(data[i].teu);
							     }
							}
						})
				  	myChart8.data.datasets.forEach((dataset) => {
				  		data.labels=ctx7_labels1;
				        dataset.data =ctx7_data1;
				    });
				    myChart8.update();
				}, 3600000);
        	}
    });
}
//货物比重(全国)
var goodsCNSpecificWeight =function(){
	    $.ajax({
	        type: "post",
	        async: true,
	        url: "/enReport/goodsCNSpecificWeight",
	        data: parameter,
	        dataType: "json",
	        success: function(data){
			    ctx7_labels=[];
			    ctx7_data=[];
			    total=0;
			    four=0;
			     for(var i=0; i<data.length;i++){
			     	if(i<4){
			     		ctx7_labels.push(data[i].goodsname);
			     		ctx7_data.push(data[i].qty);
			     		four+=data[i].qty;
			     	}
			     	 total+=data[i].qty;
			     }
			     ctx7_labels.push("其他");
			    ctx7_data.push(total-four);
			    var ctx7 = document.getElementById('chart7').getContext('2d');
				var myChart7 = new Chart(ctx7, {
					type: 'pie',
					data: {
						labels: ctx7_labels,
						datasets: [{
							data: ctx7_data,
							backgroundColor: [
								"rgba(94,151,200,0.5)",
								"rgba(25,198,198,0.5)",
								"rgba(202,127,50,0.5)",
								"rgba(206,120,222,0.5)",
								"rgba(212,228,249,0.5)",
							],
							borderColor: [
								"rgba(94,151,200,1)",
								"rgba(25,198,198,1)",
								"rgba(202,127,50,1)",
								"rgba(206,120,222,1)",
								"rgba(212,228,249,1)",
							],
							borderWidth: '2',
							pointRadius: '10',
							hoverBorderWidth: '0',
							hoverBorderColor: ['#2185da', '#2185da', '#2185da'],
						}],
					},
					options: {
						legend: {
							display: true,
							labels: {
								filter: function(legenditem,data){
									if(legenditem.text!="其他"){
										return true;
									}
								}
							}
						},
					},
				});
					setInterval(function () {
						$.ajax({
					        type: "post",
					        async: true,
					        url: "/enReport/goodsCNSpecificWeight",
					        data: parameter,
					        dataType: "json",
					        success: function(data){
							    ctx7_labels=[];
							    ctx7_data=[];
							    total=0;
				   				four=0;
							     for(var i=0; i<data.length;i++){
							     	if(i<4){
							     		ctx7_labels.push(data[i].goodsname);
							     		ctx7_data.push(data[i].qty);
							     		four+=data[i].qty;
							     	}
							     	 total+=data[i].qty;
							     }
							     ctx7_labels.push("其他");
							    ctx7_data.push(total-four);
							}
						})
				  	myChart7.data.datasets.forEach((dataset) => {
				  		data.labels=ctx7_labels;
				        dataset.data =ctx7_data;
				    });
				    myChart7.update();
				}, 3600000);
        	}
    });
}
//外贸总订舱量
var foreignBookingQuantityAll=function(){
	 $.ajax({
	        type: "post",
	        async: true,
	        url: "/enReport/foreignBookingQuantityAll",
	        data: parameter,
	        dataType: "json",
	        success: function(data){
			var counter=$('.counter1').jOdometer({
				increment: 1,
				counterStart:"0000000"+teus, 
				easing:'linear',
				numbersImage: 'images/jodometer-numbers.png', 
			  	spaceNumbers: 1, 
			  	heightNumber: 64,
			  	widthNumber:32,
			  	spaceNumbers:10,
			  	formatNumber:true,
			});
		  counter.goToNumber(data[0].teu);
		  teus =data[0].teu;
		  setTimeout("foreignBookingQuantityAll()", 60000);
        	}
    });
}
//客户订舱量前十名
var customerTopTen =function(){
	$.ajax({
	        type: "post",
	        async: true,
	        url: "/enReport/customerTopTen",
	        data: parameter,
	        dataType: "json",
	        success: function(data){
		      var str="";
		      for(var i=0;i<data.length;i++){
		      	str+='<li>'+
						'<p class="status">'+(i+1)+'</p>'+
						'<p class="amount">'+data[i].cusname+'</p>'+
						'<p class="time">'+data[i].teu+'</p>'+
					'</li>'
		      }
		      $("#ul_1").html(str);
        	}
    });
}
